package com.iwomy.envpro.platform.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.envpro.common.core.util.R;
import com.iwomy.envpro.common.log.annotation.SysLog;
import com.iwomy.envpro.platform.entity.CodeAreaEntity;
import com.iwomy.envpro.platform.service.CodeAreaService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.envpro.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 行政区划
 *
 * @author pig
 * @date 2023-08-25 15:22:34
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/codeArea" )
@Tag(description = "codeArea" , name = "行政区划管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class CodeAreaController {

    private final  CodeAreaService codeAreaService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param codeArea 行政区划
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
//    @PreAuthorize("@pms.hasPermission('platform_codeArea_view')" )
    public R getCodeAreaPage( Page page, CodeAreaEntity codeArea) {
        return R.ok(codeAreaService.page(page,  Wrappers.query(codeArea)));
    }


    /**
     * 通过id查询行政区划
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('platform_codeArea_view')" )
    public R getById(@PathVariable("id" ) String id) {
        return R.ok(codeAreaService.getById(id));
    }

    /**
     * 新增行政区划
     * @param codeArea 行政区划
     * @return R
     */
    @Operation(summary = "新增行政区划" , description = "新增行政区划" )
    @SysLog("新增行政区划" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('platform_codeArea_add')" )
    public R save(@RequestBody CodeAreaEntity codeArea) {
        return R.ok(codeAreaService.insert(codeArea));
    }

    /**
     * 修改行政区划
     * @param codeArea 行政区划
     * @return R
     */
    @Operation(summary = "修改行政区划" , description = "修改行政区划" )
    @SysLog("修改行政区划" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('platform_codeArea_edit')" )
    public R updateById(@RequestBody CodeAreaEntity codeArea) {
        return R.ok(codeAreaService.updateById(codeArea));
    }

    /**
     * 通过id删除行政区划
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除行政区划" , description = "通过id删除行政区划" )
    @SysLog("通过id删除行政区划" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('platform_codeArea_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(codeAreaService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param codeArea 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
//    @PreAuthorize("@pms.hasPermission('platform_codeArea_export')" )
    public List<CodeAreaEntity> export(CodeAreaEntity codeArea) {
        return codeAreaService.list(Wrappers.query(codeArea));
    }
	/**
	 * 没分页的列表
	 * @param codeArea 查询条件
	 */
	@GetMapping("/list")
	public R  list(CodeAreaEntity codeArea) {
		return R.ok(codeAreaService.list(Wrappers.query(codeArea)));
	}

    /****************app接口***********************/

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param codeArea 行政区划
	 * @return
	 */
	@Operation(summary = "分页查询" , description = "分页查询" )
	@GetMapping("/appCodeAreaPage" )
	public R appCodeAreaPage( Page page, CodeAreaEntity codeArea) {
		return R.ok(codeAreaService.page(page,  Wrappers.query(codeArea)));
	}




}